package com.zhoug.common.utils.qrcode;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;

import org.json.JSONObject;

import java.util.Map;

import androidx.annotation.Nullable;

/**
 * 常用二维码解析器
 *
 * @Author 35574
 * @Date 2022/1/5
 * @Description
 */
public class QRResultParser {
    private static final String TAG = ">>>QRResultParser";
    private static boolean debug = true;

    /**
     * @param result 二维码扫码出来的发票数据
     *
     *               eg:01,10,012002100111,52404770,977.09,20211021,17820145899844780497,07CF,
     *               eg:01,01,6100162130,04697626,9472.99,20170508,,86AF
     *               eg:01,04,3700172350,01891260,432946.00,20180523,70359877702251449985,8BBA
     */
    public static @Nullable
    Fapiao parseFapiao(String result) {
        if (!StringUtils.isNullOrEmpty(result)) {
            try {
                result = result.trim();
                if (result.endsWith(",")) {
                    result = result.substring(0, result.length() - 1);
                }
                if (debug) {
                    LogUtils.d(TAG, "result:" + result);
                }
                Fapiao fapiao = new Fapiao();
                String[] split = result.split(",");
                if (debug) {
                    LogUtils.d(TAG, "length:" + split.length);
                }
                if (split.length >= 7) {
                    fapiao.setType(split[1]);
                    fapiao.setCode(split[2]);
                    fapiao.setNumber(split[3]);
                    fapiao.setMoney(Double.parseDouble(split[4]));
                    fapiao.setDate(split[5]);
                    fapiao.setCheckCode(split[6]);
                }
                return fapiao;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
